<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>大黄蜂汽车租赁</title>
    <script type='text/javascript' charset='utf-8' src='https://www.layuicdn.com/layui-v2.5.6/layui.js'></script>
    <link rel='stylesheet' type='text/css' href='https://www.layuicdn.com/layui-v2.5.6/css/layui.css' />
    <script type="text/javascript" charset='utf-8' src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body style="background-color: #F2F2F2">
<div class="layui-row" style="margin: 20px;">
    <div class="layui-card">
        <div class="layui-card-header"><i class="layui-icon layui-icon-search"></i>查询条件</div>
        <div class="layui-card-body layui-row">
            <form class="layui-form layui-form-pane layui-col-xs6">
                <div class="layui-form-item">
                    <label class="layui-form-label">关键字</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input" name="keyword" autocomplete="off">
                    </div>
                </div>
                <div class="layui-form-item" pane>
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="type" autocomplete="off" value="0" title="出租单号" checked>
                        <input type="radio" name="type" autocomplete="off" value="1" title="身份证号">
                        <input type="radio" name="type" autocomplete="off" value="2" title="汽车牌照">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="search">搜索</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="layui-row">
    <div class="layui-row" style="margin-left: 20px;margin-right: 20px;">
        <div class="layui-card">
            <div class="layui-card-header">出租单管理</div>
            <div class="layui-card-body layui-row">
                <table id="rents" lay-filter="rents"></table>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="rightBar">
    <a class="layui-btn layui-btn-xs" lay-event="update">修改归还日期</a>
</script>
<script type="text/html" id="rentDialog">
    <div class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <label class="layui-form-label">单号</label>
            <div class="layui-input-block">
                <input type="text" value="{{ d.id }}" class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">身份证号</label>
            <div class="layui-input-block">
                <input type="text" value="{{ d.customerIdentity }}" class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车辆车牌</label>
            <div class="layui-input-block">
                <input type="text" value="{{ d.carNumber }}" class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">出租价格</label>
            <div class="layui-input-block">
                <input type="text" value="{{ d.price }}" class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">起租时间</label>
            <div class="layui-input-block">
                <input type="text" id="beginTime" value="{{ d.beginTime }}" class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">还车时间</label>
            <div class="layui-input-block">
                <input type="text" id="returnTime" value="{{ d.returnTime }}" class="layui-input">
            </div>
        </div>
    </div>
</script>
<script type="application/javascript">
    layui.use(['table', 'layer', 'form', 'laytpl', 'laydate'], function () {
        let table = layui.table;
        let layer = layui.layer;
        let form = layui.form;
        let laytpl = layui.laytpl;
        let laydate = layui.laydate;
        table.render({
            id: 'rents',
            elem: '#rents',
            url: '../rent/all', //数据接口
            title: '客户数据表',
            page: true, //开启分页
            limit: 9,
            limits: [9, 18, 27, 35, 45, 54],
            toolbar: true,
            defaultToolbar: ['filter', 'print', 'exports'],//工具条
            cols: [ [ //表头
                {field: 'id', title: '出租表单号', width:280},
                {field: 'customerIdentity', title: '身份证号', width:220},
                {field: 'carNumber', title: '车牌号', width:120},
                {field: 'price', title: '出租价格', width:120, sort: true},
                {field: 'beginTime', title: '起租时间', width:120, sort: true},
                {field: 'returnTime', title: '还车时间', width:120, sort: true},
                {fixed: 'right', title:'操作', toolbar: '#rightBar'}
            ] ],
            response: {
                statusCode: 200, //规定成功的状态码，默认：0
            },
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.total, //解析数据长度
                    "data": res.data.record //解析数据列表
                };
            }
        });
        //监听行工具事件
        table.on('tool(rents)', function(obj) {
            var data = obj.data;
            if (obj.event === 'update') {
                let rentDialog = $('#rentDialog').html();
                laytpl(rentDialog).render(data, function(html) {
                    layer.open({
                        type: 1,
                        area: ['500px', '450px'],
                        title: '出租单号',
                        closeBtn: 0,
                        shadeClose: true,
                        content: html,
                        btn: ['确定', '取消'],
                        yes: function (index) {
                            let initTime = data.returnTime;
                            let returnTime = $("#returnTime").val();
                            if(initTime == returnTime){
                                layer.msg("日期相同，未做修改");
                                layer.close(index);
                                return true;
                            }
                            $.post('../rent/update/returnTime', {
                                id: data.id,
                                returnTime: returnTime
                            }, function (res) {
                                if (res.code == 200){
                                    obj.update({
                                        returnTime: returnTime
                                    })
                                    layer.msg('修改成功', {
                                        icon: 1
                                    })
                                }else if (res.code == 504){
                                    layer.layer('修改日期解析错误');
                                }else if (res.code == 505){
                                    layer.alert("修改日期错误");
                                }else if (res.code == 404){
                                    layer.alert("出租单未找到");
                                }else if (res.code == 500){
                                    layer.alert('服务器错误')
                                }
                            }, 'json');
                            layer.close(index);
                            return true;
                        },
                        btn2: function (index) {
                            layer.close(index);
                        },
                        success: function () {
                            laydate.render({
                                elem: '#beginTime' //指定元素
                            });
                            laydate.render({
                                elem: '#returnTime',
                                min: new Date().getTime()
                            });
                        }
                    });
                });
            }
        });

        form.on('submit(search)', function (data){
            let keyword = data.field.keyword;
            let type = data.field.type;
            table.reload('rents', {
                url: '../rent/search',
                where: {
                    type: type,
                    keyword: keyword
                },
                page: {
                    curr: 1
                }
            })
            return false;
        })
    })
</script>
</html>